package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.common.ServerResponse;
import com.situ.mall.dao.ProductMapper;
import com.situ.mall.entity.Product;
import com.situ.mall.service.IProductService;

@Service
public class ProductServiceImpl implements IProductService{
	@Autowired
	private ProductMapper productMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit, Product product) {
		PageHelper.startPage(page, limit);
		
//		ProductExample productExample = new ProductExample();
//		Criteria criteria = productExample.createCriteria();
//		if (product.getId() != null) {
//			criteria.andIdEqualTo(product.getId());
//		}
//		if (product.getName() != null && !"".equals(product.getName()) ) {
//			criteria.andNameLike("%" + product.getName() + "%");
//		}
		
		List<Product> list = productMapper.pageList(product);
		PageInfo pageInfo = new PageInfo(list);
		int count = (int) pageInfo.getTotal();
		return ServerResponse.createSuccess("", count, list);
	}

	@Override
	public ServerResponse deleteById(Integer id) {
//		ProductExample productExample = new ProductExample();
//		Criteria criteria = productExample.createCriteria();
//		criteria.andIdEqualTo(id);
//		int count = productMapper.deleteByExample(productExample);
		
		int count = productMapper.deleteByPrimaryKey(id);
		if (count != 1) {
			return ServerResponse.createError("删除失败");
		}
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public ServerResponse insert(Product product) {
		int count = productMapper.insert(product);
		if (count != 1) {
			return ServerResponse.createError("添加失败");
		}
		return ServerResponse.createSuccess("添加成功");
	}

	@Override
	public Product selectById(Integer productId) {
		return productMapper.selectByPrimaryKey(productId);
	}


}
